package com.lightsh.gui;

import org.jbox2d.common.Vec2;

import com.lightsh.action.ApplyForce;
import com.lightsh.body.RectangleImpl;
import com.lightsh.graphics.ImageUtility;
import com.lightsh.jbox2d.R;
import com.lightsh.things.BulletCircle;
import com.lightsh.things.Gun;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

public abstract class BaseGameActivity extends Activity{
	protected GameSurfaceView mSurfaceView; 
	protected BaseScreen mScreen;	
	private SensorEventListener mSensorLitener;
	
	/**
	 * 需要在该方法中初始化{@link #mScreen}信息
	 */
	protected abstract void initScreen();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);		
		mSurfaceView = new GameSurfaceView(this);
		
		initScreen();
		
		// 设置负责游戏绘制和事件处理的screen
		mSurfaceView.setGameScreen(mScreen);				
		setContentView(mSurfaceView);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
		checkSensorState(false);
	}

	@Override
	protected void onResume() {
		super.onResume();
		checkSensorState(true);
	}
	private void checkSensorState(boolean isOpen){
		try{
			SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
			if(isOpen && mScreen != null && mScreen.isSensorEnable()){
				if(mSensorLitener == null){
					mSensorLitener = new SensorEventListener() {
						
						@Override
						public void onSensorChanged(SensorEvent event) {
							if(mScreen != null){
								mScreen.onSensorChange(event);
							}							
						}
						
						@Override
						public void onAccuracyChanged(Sensor sensor, int accuracy) {
							
						}
					};
				}
				sm.registerListener(mSensorLitener, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
			}else{
				if(mSensorLitener != null){
					sm.unregisterListener(mSensorLitener);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return mScreen.onKeyDown(keyCode, event) ? true : super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		return mScreen.onKeyUp(keyCode, event) ? true : super.onKeyUp(keyCode, event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, "");
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MenuItem item = menu.getItem(0);
		if(mScreen.isPause()){
			item.setTitle(getString(R.string.begin));
		}else{
			item.setTitle(getString(R.string.pause));
		}
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case 0:{
			if(mScreen.isPause()){
				mScreen.resume();
			}else{
				mScreen.pause();
			}
			break;
		}
		}
		return true;
	}
}
